Description:
Calling an abstract method from an abstract class constructor
may result in execution of methods declared in subclasses before the initialization
of the object is completed.
Incorrect:
In the following example, an attempt to create a WordPair object
will result in a NullReferenceException being thrown.
WordSequence = class abstract
strict protected
length:Integer;
public
procedure computeLength();virtual;abstract;
constructor Create;
end;
WordPair = class(WordSequence)
strict private
first:String;
second:String;
public
procedure computeLength();override;
constructor Create(first,second:String);
end;
...
constructor WordSequence.Create;
begin
inherited;
computeLength();
end;
constructor WordPair.Create(first,second:String);
begin
inherited Create;
self.first := first;
self.second := second;
end;
procedure WordPair.computeLength();
begin
length := first.Length + second.Length;
end;